package data;

import java.util.List;
import java.util.ArrayList;
import java.util.Vector;

/**
 * 游戏运行时数据管理类
 */
public class GameRuntimeData {
    // -------------------- 数据设计 --------------------
    // 游戏状态
    private int gameState = 0;          // 0菜单1游戏中2暂停3结束
    private boolean gameRunning = false;
    private int currentGrade = 1;
    private int score = 0;

    // 游戏对象
    private HeroData hero;
    private Vector<EnemyData> enemyPool;
    private List<BlockData> blockList;
    private List<BombData> bombList;
    private CampData camp;

    // 游戏统计
    private int enemyTotalNum = 10;
    private int enemyDeadNum = 0;
    private long gameStartTime;
    // -------------------- 数据设计 --------------------

    // 单例模式
    private static GameRuntimeData instance;

    private GameRuntimeData() {
        initializeData();
    }

    public static GameRuntimeData getInstance() {
        if (instance == null) {
            instance = new GameRuntimeData();
        }
        return instance;
    }

    private void initializeData() {
        enemyPool = new Vector<>();
        blockList = new ArrayList<>();
        bombList = new ArrayList<>();

        // 预创建爆炸效果池
        for (int i = 0; i < 20; i++) {
            bombList.add(new BombData(0, 0));
        }
    }

    public void resetGameData() {
        // 重置坦克
        if (hero != null) {
            hero.setState(0);
            hero.setHp(100);
        }

        // 重置敌方坦克池
        for (EnemyData enemy : enemyPool) {
            enemy.setState(0);
            enemy.setHp(100);
        }

        // 清空方块列表
        blockList.clear();

        // 重置爆炸效果
        for (BombData bomb : bombList) {
            bomb.setState(0);
            bomb.setBombProgress(0);
        }

        // 重置统计数据
        enemyDeadNum = 0;
        gameStartTime = System.currentTimeMillis();
    }

    public void createHero() {
        hero = new HeroData(560, 750, 0);
        hero.setState(1);
    }

    public void createCamp() {
        camp = new CampData(560, 800);
    }

    public BombData getAvailableBomb() {
        for (BombData bomb : bombList) {
            if (bomb.getState() == 0) {
                return bomb;
            }
        }
        return null;
    }

    // getter和setter方法
    public int getGameState() { return gameState; }
    public void setGameState(int gameState) { this.gameState = gameState; }
    public boolean isGameRunning() { return gameRunning; }
    public void setGameRunning(boolean gameRunning) { this.gameRunning = gameRunning; }
    public int getCurrentGrade() { return currentGrade; }
    public void setCurrentGrade(int currentGrade) { this.currentGrade = currentGrade; }
    public int getScore() { return score; }
    public void setScore(int score) { this.score = score; }

    public HeroData getHero() { return hero; }
    public Vector<EnemyData> getEnemyPool() { return enemyPool; }
    public List<BlockData> getBlockList() { return blockList; }
    public List<BombData> getBombList() { return bombList; }
    public CampData getCamp() { return camp; }

    public int getEnemyTotalNum() { return enemyTotalNum; }
    public void setEnemyTotalNum(int enemyTotalNum) { this.enemyTotalNum = enemyTotalNum; }
    public int getEnemyDeadNum() { return enemyDeadNum; }
    public void setEnemyDeadNum(int enemyDeadNum) { this.enemyDeadNum = enemyDeadNum; }
    public void incrementEnemyDeadNum() { this.enemyDeadNum++; }

    public long getGameStartTime() { return gameStartTime; }
}